import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    routePath: "",
    titleName:"",
    path:"",
    targetName:"",
    editableTabsValue: "0",
    editableTabs: [],
    tabIndex: 0,
    num:0
  },
  mutations: {
    removeTab() {
      let tabs = this.state.editableTabs;
      let activeName = this.state.editableTabsValue;
      if (activeName === this.state.targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === this.state.targetName) {
            let nextTab = tabs[index + 1] || tabs[index - 1];
            if (nextTab) {
              activeName = nextTab.name;
            }
          }
        });
      }
      this.state.editableTabsValue = activeName;
      this.state.editableTabs = tabs.filter(tab => tab.name !== this.state.targetName);
    },
    addTab() {
      let newTabName = this.state.tabIndex++ + '';
      this.state.editableTabs.push({
        title: this.state.titleName,
        name: newTabName,
        content: this.state.routePath,
        num:this.state.num,
      });
      this.state.num++;
      this.state.editableTabsValue = newTabName;
    },
  },
  actions: {

  },
  getters:{

  },
})
